package com.jumi.microservice.interfaces;

import com.jumi.microservice.common.base.dto.BaseRequest;
import com.jumi.microservice.common.base.dto.BaseResponse;
import com.jumi.microservice.dto.PaymentResponse;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Dirk
 * @Date 2020-07-20 13:46
 */
public abstract class BasePayment implements Payment {

    public static Map<String, BasePayment> paymentMap = new ConcurrentHashMap<>();

    @PostConstruct
    public void init() {
        paymentMap.put(getPayChannel(), this);
    }

    @Override
    public BaseResponse process(BaseRequest request) {
        //创建上下文
        PayContext context = createContext(request);
        //前置逻辑处理
        preProcess(context);
        //基本业务处理
        BaseResponse response = coreProcess(context);
        //后续业务处理
        afterProcess();

        return response;
    }

    /**
     * 创建支付上下文
     *
     * @param request
     * @return
     */
    public abstract PayContext createContext(BaseRequest request);

    /**
     * 前置处理逻辑
     *
     * @param context
     */
    public abstract void preProcess(PayContext context);

    /**
     * 核心处理逻辑
     *
     * @param context
     * @return
     */
    public abstract BaseResponse coreProcess(PayContext context);

    /**
     * 后续处理逻辑
     */
    public abstract void afterProcess();

    /**
     * 获取支付渠道
     *
     * @return
     */
    public abstract String getPayChannel();

    /**
     * 退款
     *
     * @param request
     * @return
     */
    public abstract PaymentResponse refund(BaseRequest request);
}
